Sonar mapping




LABSIS

2019


Autores:


Michele Gaiarin (Nº 1190274)

1190274@isep.ipp.pt


Antonio Domingos Dias Santos (Nº 1171178)

1171178@isep.ipp.pt


Introdução

Arquitetura

Hardware

Software

Resultados

Conclusões

Referências




  • Introdução

The request made for this project was to obtain a system capable of mapping a room, determining the distance of objects and walls from where the assessment is made. Furthermore, the system must be able to create a 2D map with the measures toke and to show them visually via the graphical interface.
For the development have been used a sonar that allows you to measure the distance, a stepper motor to be able to rotate the sensor, so it's possible to acquire the measurements at 360° and a USB to serial converter module (CP2102) to send the collected data to the PC via UART.
On the PC side was used Python and Qt to create a very simple interface able to:

  • Enable the UART connection
  • Send a signal to enable the measurements
  • Show a plot of the measurements took before

State of the Art

Sonar, (from "sound navigation ranging"), technique for detecting and determining the distance and direction of underwater objects by acoustic means. Sound waves emitted by or reflected from the object are detected by sonar apparatus and analyzed for the information they contain [1].

SONAR applications in mapping

Let`s see some example of application already made.

Ultrasonic Cave Mapping

This study uses a novel method based on ultrasound reflections to produce an accurate 3D model of Kitley Shelter Cave [Figure 1], an archaeological cave in Yealmpton, Devon. This model enables the cave system to be visualized leading to a better understanding of the site and its taphonomy. [2] A number of options are available for automatically calculating the distance of an object from a point. [3]The most accurate systems use electromagnetic radiation (either radar or laser sources). [2] Laser sources are highly dependent on the precise reflective properties of the target object and are unsuitable when used on rough, bare rock surfaces. [2] Radar is considerably better, but suitable equipment is neither cheap nor easily portable. Sound waves are a suitable alternative for range finding where exact precision is not required. [4] Ultrasound transducers are relatively inexpensive, and usually ultrasound beams reflect well off rock surfaces. The lack of precision is due to the difficulty in obtaining a narrow ultrasound beam and the problem of shaping a precisely defined sound pulse.

Cave mapping
Fig.1 - Fully rendered 3D representation of Kitley Shelter Cave.

Implemented prototypes

Ultrasonic Map-Maker using an Arduino Yun [5]

Project developed and implemented by Satyavrat Wagle, makes use of the HC-SR04 ultrasonic sensor to map the surrounding environment. The sensor is attached to a servo motor (Tower Pro SG90) that makes it spin, all the while the Arduino Yun controls the motion of the servo motor [Figure 2]. The process of data from the sensor and subsequent plotting of the graphical map [Figure 3]. is achieved by using Mathworks MatLab.

Final project
Fig.2 -Final project.
plotted graph
Fig.3 - Plotted graph.

Início








  • Arquitetura

Sonar Architecture Schema

Sonar Architecture
Fig.4 - Sonar Architecture.

Blocks description

Atmega 328p:

The microcontroller deals with :

  • Driving the stepper motor: By configuring the PINC [0/1/2/3] the ULN2003 driver is configured which in turn drives the motor with the corresponding coil configuration.
  • Activate and deactivate Timer0 to time the rotation of the motor.
  • Communicate with the CP2102 module to send and receive messages via USART through the RXE and TXE PINs. In particular, it receives a character ('A') to start the mapping. Subsequently, it takes care of communicating with the CP2102 module to send the data acquired during the measurement. Note that the data is not sent directly but is first converted by "double" a "string". the strings obtained are concatenated by creating the special string: "degreeValueString;distanceValueString\n".
    This string will then be sent to the PC.
  • Activate the sonar: Operation that is performed by activating the PIN trigger (I / O) of the sonar for 15us (At least 10us is required).
  • Calculate the distance of an object: This operation is done by activating Timer1 when the Echo PIN (I / O) connected to the sonar detects a rising edge and saves the counter value when it detects a falling edge. Once the value is obtained, it is converted to [cm].

Motor:

The motor rotates the sensor during data acquisition. Every 5,625° (Every 64 steps) set the flag to enable the measurement through sonar. During the measurement it maintains the current position and starts to rotate again after the data has been sent to the PC.

Sonar:

Sonar is provided by the microcontroller via the PIN trigger, which must remain set for at least 15us. After sonar is enabled set the echo PIN and send the signal. When the signal goes back and hits the receiver it resets the echo PIN.

CP2102:

CP2102 is used to receive the start measurement signal (the character 'A') from the PC (via interrupt). When this character is read room mapping begins.
Subsequently, serial connection is used to transmit the string, previously created by the microcontroller, with data relating to the position of the motor and the measured distance.

PC side:

On the PC side through a program developed in Python it is possible to create the serial connection with the microcontroller. Once the connection is established, is possible to send the signal to start the measurement. Subsequently, the program takes care of reading the strings received from the serial port. The position and distance data are extracted from the string using regular expressions that eliminate special characters and save all the data in a list, which will then be used to create the map via the python library: "matplotlib.pyplot"

Início








  • Hardware

The whole system requires a current consumption of 280mA. You will now see in detail all the components used.

Atemga328p
Fig.5 - ATmega328p.

Microcontroller: atmega328p

The ATmega328 is a 8-bit AVR microcontroller created by Atmel (now part of Microchip Technology Inc.) in the megaAVR family. Among other features, the ATmega328p has 32 kilobytes (kB) of program memory size, 1kB EEPROM, 2kB SRAM, 27 general purpose I/O lines, 32 general purpose working registers, five flexible timer/counters with compare modes, internal and external interrupts, two USARTs, two byte-oriented 2-wire serial interfaces, two SPI serial ports, 8-channel 10-bit A/D converter[6]. The most common implementation of this chip is in the Arduino development boards, used mostly by hobbyist and in educational embedded applications.

hcsr04
Fig.6 - HC-SR04.

SONAR: HC-SR04 sensor

The HCSR04 ultrasonic sensor uses sonar to determine distance to an object. This module provides 2cm to 400cm measurement function, ranging accuracy of 3mm and a maximum angle at which objects are positioned and still retrieve an accurate measure (effectual angle) of 15°. The modules includes ultrasonic transmitters, receiver and control circuit. The ultrasonic distance measurement principle works as followed: when enabled, the ultrasonic transmitter of the module sends an ultrasonic pulse train (eight 40 kHz pulses) in one direction, which returns to the ultrasonic receiver when it encounters an obstacle in its way. Considering the velocity of sound constant (340 m/s), the information about the distance is strictly pertained to the elapsed time. With a known elapsed time, the distance of the object can be calculated using the formula:[7]
Distance (d) = Velocity of Sound (v) x Elapsed Time (t) / 2.

motor
Fig.7 - Motor 28BYJ-48.

Motor: 28byj-48 stepper motor

Stepper motors are a special type of brushless motors that divides a full rotation into a number of equal steps. They are usually found in desktop printers, 3D printers, CNC milling machines, DVD players, and anything else that requires precise positioning control. Stepper motors will move only one step at a time. These motors have a sequence of coils present in them and these coils have to be energized in a particular fashion to make the motor rotate. When each coil is being energized the motor takes a step and a sequence of energization will make the motor take continuous steps, thus making it to rotate. The 28byj-48 stepper moto has a 4 coil unipolar arrangement and each coil is connected to a constant +5V supply. The coil will only be active if the other end of the coil is grounded. A stepper motor can be made to rotate only if the coils are energized (grounded) in a logical sequence. This logical sequence will be programmed using a microcontroller. This motor has a speed variation ratio of 1/64, which means that the shaft on the outside will make one complete rotation only if the motor inside rotates for 64 times. This is because of the gears that are connected between the motor and output shaft and these gears help in increasing the torque. When working in half-step mode, this motor has a stride angle of 5.625°/64, so it will have to make 64 steps to complete one rotation and for every step it will cover a 5.625°. The power consumption of the motor is around 240mA.
The motor consumes power, even in the stand still state, to maintain its position.[8][9]

Driver motor
Fig.8 - Driver motor ULN2003.

Motor driver: ULN2003

The ULN2003A is a high voltage, high current Darlington arrays each containing seven open collector common emitter pairs. Each pair is rated at 500mA. [datasheet reference] A Darlington transistor configuration is an array of bipolar transistors pairs, connected in such a way that the current amplified by the first transistor is amplified further by the second one [x]. The motor that is used in this project consumes high current, so a driver is needed to be able to control the motor with a microcontroller. As mentioned previously, the Darlington configuration present in this IC driver allows for high current output with low current and voltage prevenient from the microcontroller[10][11].

USBASP
Fig.9 - USBasp.

Programmer: USBasp

The AVR USBasp is a USB in-circuit programmer that can use to program most of the Atmel AVR controllers. It simply consists of an ATMega8 and a couple of passive components such as resistors, capacitors, LEDs and ect. The programmer uses a firmware only USB driver and there is no special USB controller is needed. By using AVR USBasp, it is easier and simpler, it just needs one step to finish the process which is to connect the AVR USBasp with computer and with microcontroller, then program it.[datasheet reference] The module has 3.3V and 5V double voltage supports, with 500mA over current protection re settable fuse to prevent damage to the computer USB port connected with[12].

USB-to-TTL
Fig.10 - USB to TTL converter CP2012.

USB to TTL converter: CP2102

The CP2102 is a highly-integrated USB-to-UART Bridge Controller, made by Silicon Laboratories, providing a simple solution for updating RS-232 designs to USB.[datasheet reference] The embedded system converts the differential D+ and D- signals of the USB interface in to RX and TX signals of the RS232 interface and this output is TTL (transistor-transistor logic) compatible. This TTL is the method of serial communication used by UART. The USB interface can run with lower input and supply voltages, but the RS232 output will be TTL level (between ground and 3.3V) [13][14].

PCB

PCB
Fig.11 - PCB.

Module schematics:

Module schema
Fig.12 - Module schema.

Início








  • Software

Flow chart schema

Flow chart
Fig.13 -Flow chart.

Functions description

Initialization

initializzation sonar_init usart_init motor_calc motor_init

Initializzation: Here is possible to see al the functions used for initilize the Atmega328p with all the comment for undestand better wht every function do.
Timer0 (The one that control the motor) was set for moving every 5ms. On the picture is possible to see the calculation made, where:

  • T_ocr : interval of time between interruptions;N : prescaler selected
  • F_clk : frequency that the microcontroller is working on

rx_vect

ISR(USART_RX_vect) : When receve an interrupt from the USART check the data. If the data is the character 'A' then reset the flags "interrupt_counter" and "last_step" also enable the Timer0.

timer0_a

ISR(TIMER0_COMPA_vect) :When an interrupt occurs modify the motor's coil configuration to the next step.
After that the function check if :

  • The engine has reached one of the positions where you need to measure the distance with the sonar. If it has reached the flag set "activate_sonar".
  • If the flag used to configure the coils must be reset or increased.
  • If rotation has completed. If it was completed:
    • sends the end of communication signal to the PC (character 'E').
    • Change the Timer0's comparator.
    • Reset the flag "degree_multiplier"

timer0_b

ISR(TIMER0_COMPB_vect) : Return the engine to the initial position through an inverse rotation with respect to the one in which the measurements were taken. It also checks every time if the original position has been reached. If the condition is verified, it changes the comparator of Timer0 and set the flag "last_step" to indicate that the position has been reached.

timer1_overflow

ISR(TIMER1_OVF_vect) : Whenever there is an overflow in the counter of Timer1 it increases the value of the flag "TimerOverflow".

distance

Distance_Measurement() : This function takes care of enabling sonar through the PIN Trigger.
Then set the input capture PIN of Timer1 (ICP1) in rising edge mode. When the sonar PIN echo goes to HIGH state the timer counter is reset and the input capture PIN is set in falling edge mode. At this point, when the sonar PIN echo goes to the LOW state, the counter value is saved in a variable. This value is added with the value:
TimerOverflow * 65535.
The result obtained is the total counter. Finally, this value is converted into centimeters and returned by the function.
The convertion is calculated with the formula:
Distance = (SoundSpeed * TIMER) / 2
The TIMER works at 8Mhz and the sound speed = 343m/s
So the result is :
Distance [cm] = TIMER value / 464.

send_data

Send_Data() : The purpose of this function is to convert the data obtained from the measurement in strings. Then concatenate everything to get a string in the format "degreeValueString;distanceValueString\n". Finally send the string obtained using the USART_SendString () function which takes care of sending strings through the USART taking advantage of the USART_TxChar function which sends single characters through the USART.

main

Main : The main program continuously checks whether the "activate_sonar" and "last_step" are respectively set and reset. If the condition has occurred :

  • Disable Timer0 (stop the motor in the current position).
  • By taking advantage of the function Distance_Measurement() Performs 10 distance measurements and calculates the average, saving the result in a variable.
  • Calculate and save in a variable the angle where the motor is positioned, multiplying the value 5.625 * & quot; degree_multiplier & quot ;. NOTE:
    Measurements take place every 5,655° The flag "degree_multiplier" takes into account how many measurements have already been made.
  • By taking advantage of the function Send_Data() sends the data to the PC.
  • Reset the flag "activate_sonar" and increment the flag "degree_multiplier".
  • Finally enable Timer0 again.
If the condition is not verified, check if the flag "last_step" is set. If is set it disable the Timer0.

python

Send_Data() : The purpose of this function is to convert the data obtained from the measurement in strings. Then concatenate everything to get a string in the format "degreeValueString;distanceValueString\n". Finally send the string obtained using the USART_SendString () function which takes care of sending strings through the USART taking advantage of the USART_TxChar function which sends single characters through the USART.

rx_vect

Python program : In the PC side the program was made with Python3. The program is a class capable of creating a simple graphical interface, with which it is possible :

  • Choose the connection via a drop-down menu.
  • Create the connection with the microcontroller and verify that it works.
    The connection is created using the Python's library "serial".
  • Send the command to start the mapping via a button.
Once the mapping has started, the program reads the string received from the microcontroller from the serial port.
Motor position and measured distance values are extracted from the string using regular expressions that eliminate special characters and divide the string in two using the symbol as separator ";" previously inserted by the microcontroller. Once obtained, the data are converted from string to float and saved in two lists.
Finally the values in the lists are plotted using the Python library "matplotlib.pyplot" showing the polar plot and displaying the results achieved.

Início








  • Resultados

Final project
Fig.14 - Final Project.
Sonar GUI
Fig.15 - Sonar graphical interface.
Sonar GUI
Fig.16 - Sonar result map.

Início








  • Conclusões

Objectives evaluation:

The objectives proposed were as followed: to make a system able of mapping a room, determining the distance of objects and walls from where the assessment is made, and furthermore present a 2D map with the measures toke and to show them visually via the graphical interface. The main scope of this system was achieved, never the less with some limitations. In theory, the implemented system is not able to map a room bigger then 4mx4m because of the technical limitations imposed by the HC-SR04 that has a maximum range of 4m, although that the best results obtained by this system where within the area of 2mx2m.

SONAR unreliability

The sonar module HC-SR04 was the most limiting factor of this system. As specified in the datasheet, this module works best when operating with flat surfaces. As presented in the following demonstration, this system shows good results when working with flat surfaces and even accompanies the change of distance of said surface with good results.

When operating in an environment with irregular surfaces and changing angles of incidence of the ultrasonic waves as this system requires, this module can present inaccurate results, resulting in abnormal values considering the area in question. An implemented solution to this problem was an application of the Kalman filter algorithm. This algorithm uses a series of measurements observed over time, with inaccurate readings, and makes estimates that tend to be more accurate than single measurements, in which results a smoothing of the plotting of the single point readings. Although this method provides some improvements over the single result analyses, it is prone to fail when the system encounters big disparity between successive angles, as in wrong readings by the SONAR.

Bluetooth implementation

A tested upgrade for the system was the substitution of the USART to serial converter for the Bluetooth module HC-05. The implementation of this module plus batteries would allow for a wireless connection to the exterior of the system. After various trials with this module it was reached the conclusion that this method of communication was less reliable than the wired one, as it sometimes would work as intended and some other times, many followed successful attempts, would not transmit data to the PC. The problem is it could derive directly from that specific component, having also shown problems relating to the power supply being unable to turn on and having continuous disconnections. As we were not able to find an explanation and solution for this problem, this upgrade was scraped from the final version.

Batteries implementation

As mention previously, the implementation of the Bluetooth module plus batteries would allow for a wireless connection to the exterior of the system. This implementation was tested with two 9V alkaline batteries rated as 500 mAh, connected in parallel to the entrance of a LM7805 voltage regulator circuit. There were used two batteries to increase the lifetime of each one. This implementation was scrapped because of the problem encountered with the Bluetooth module, making this way of powering the circuit redundant.

Início








  • Referências

Início

[1] The Editors of Encyclopaedia Britannica, “Sonar,” [Online]. Available : https://www.britannica.com/technology/sonar .[Acessed in 14 10 2019].

[2] W. I. Sellers e A. T. Chamberlain, "Ultrasonic Cave Mapping", Journal of Archaeological Science, vol. 25, no 9, p. 867–873, 1998.

[3] D. Gibson, "Electronics in surveying," Journal of the BCRA Cave Radio and Electronics Group, 25, no 9, p. 867–873, 1998.

[4] D. Gibson, "Accuracy problems in ultrasonic and light-beam," Journal of the BCRA Cave Radio and Electronics, vol. 6, p. 9, 1991.

[5] S. Wagle, "Ultrasonic Map-Maker using an Arduino Yun GPL3+", [Online]. Available : https://create.arduino.cc/projecthub/Satyavrat/ultrasonic-map-maker-using-an-arduino-yun-37c72e. [Acessed in 16 10 2019].

[6] www.microchip.com, " Atmega328p", [Online]. Available https://www.microchip.com/wwwproducts/en/ATmega328PB].

[7] Sprakfun, " HC-Sr04 datasheet", [Online]. Available https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf].

[8] CircuitDigest, " What is stepper motor and how it works;, [Online]. Available https://circuitdigest.com/tutorial/what-is-stepper-motor-and-how-it-works]. [Acessed in 01 10 2018].

[9] Components 101, " 28BYJ-48 datasheet", [Online]. Available https://components101.com/sites/default/files/component_datasheet/28byj48-step-motor-datasheet.pdf].

[10] CircuitDigest, " Interfacing Stepper Motor with AVR Microcontroller Atmega16;, [Online]. Available https://circuitdigest.com/microcontroller-projects/avr-microcontroller-atmega16-stepper-motor-interfacing]. [Acessed in 19 02 2019].

[11] www.diodes.com, " ULN200xA datasheet", [Online]. Available https://www.diodes.com/assets/Datasheets/ULN200xA.pdf].

[12] www.hmangas.com, " AVR-USBasp datasheet", [Online]. Available http://www.hmangas.com/Electronica/Datasheets/USBasp/AVR-USBasp%20User%20Manual.pdf].

[13] haoyuelectronics, " Schematic CP2102;, [Online]. Available http://www.haoyuelectronics.com/ebay/CP2102_module/USB-to-UART_SCH.pdf].

[14] Sparkfun, " CP2102 datasheet", [Online]. Available https://www.sparkfun.com/datasheets/IC/cp2102.pdf].

Images:

[1] W. I. Sellers e A. T. Chamberlain, "Ultrasonic Cave Mapping", Journal of Archaeological Science, vol. 25, n°r; 9, p. 867–873, 1998.

[2] S. Wagle, "Ultrasonic Map-Maker using an Arduino Yun © GPL3+", [Online]. Available https://create.arduino.cc/projecthub/Satyavrat/ultrasonic-map-maker-using-an-arduino-yun-37c72e]. [Acessed in 16 10 2019].

[3] S. Wagle, "Ultrasonic Map-Maker using an Arduino Yun GPL3+", [Online]. Available https://create.arduino.cc/projecthub/Satyavrat/ultrasonic-map-maker-using-an-arduino-yun-37c72e ]. [Acessed in 16 10 2019].

[5] robu.in "Atmega328p", [Online]. Available https://robu.in/product/atmega328p-pu-pdip-28-microcontroller/ ].

[6] SparkFun "HC-SR04", [Online]. Available https://www.sparkfun.com/products/15569].

[7] tme.eu "Step motor 28BYJ-48-5V", [Online]. Available https://www.tme.eu/pt/details/mikroe-1530/acessorios-para-sistemas-de-arranque/mikroelektronika/step-motor-5v-28byj-48-5v/?brutto=1¤cy=EUR&gclid=Cj0KCQiAxrbwBRCoARIsABEc9shSBB61nzwQ0og35DTMAXy7Xw6ExHQApseDQ_aQFrAYEkzhI-nsolsaAio9EALw_wcB ].

[8] ptrobotics.com "ULN2003", [Online]. Available https://www.ptrobotics.com/circuitos-integrados-varios/1575-uln2003-7-channel-darlington-driver.html?gclid=Cj0KCQiAxrbwBRCoARIsABEc9siddhJYzubNVIZb_jKAyO_8FiJttzhOnB8O9pfJesiBc9sc82zqMWMaAiWFEALw_wcB ].

[9] https://www.banggood.com/ "USBasp", [Online]. Available https://www.banggood.com/pt/3_3V-5V-USBASP-USBISP-AVR-Programmer-Downloader-ATMEGA8-ATMEGA128-With-Download-Cable-p-1179967.html?gmcCountry=PT¤cy=EUR&createTmp=1&utm_source=googleshopping&utm_medium=cpc_bgs&utm_content=lijing&utm_campaign=ssc-pt-all-july&ad_id=353642503365&gclid=Cj0KCQiAxrbwBRCoARIsABEc9sjz_goVF26y90hW2PpTJntGKnZRFsaDgkunzcqmzUZ1By8pzjclwbAaAvTjEALw_wcB&cur_warehouse=CN ].



Início